﻿using System.Windows.Forms;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Plumber.Masters.ModuleData;
using Plumber.Sales.ModuleData;

namespace Plumber.Sales.Scrounger
{
    public partial class TabInvoice : UserControl, iScroungerUserControl
    {
        private const string saleinvoiceSlave = "SALEINVOICESLAVE";

        public TabInvoice()
        {
            InitializeComponent();
        }

        public void OnAttachData(ApplicationLibrary.DBManager.DBManager dbManager)
        {
            dgwInvoice.AutoGenerateColumns = false;
            dgwInvoice.DataSource = dbManager.Dataset.Tables[saleinvoiceSlave];
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.InvoiceType, true);
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.InvoiceNo, true);
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.ReleaseDate, true);
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.Master, true);
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.Address, true);
            dgwInvoice.AddReadOnlyColumn(PL_SalesInvoices.Total, true);

            colTotal.Prefix = Properties.Resources.CurrencySimbol;
            colDocType.EnumsType = typeof(InvoiceType);
            colDocType.ResourceManager = Properties.Resources.ResourceManager;
        }

        public void OnBindData(ApplicationLibrary.DBManager.iScrounger frm)
        {
            DocumentForm doc = (DocumentForm)frm;
            doc.BindControl(dgwInvoice);
            doc.BindColumn(colDocType, PL_SalesInvoices.InvoiceType);
            doc.BindColumn(colInvoice, PL_SalesInvoices.InvoiceNo);
            doc.BindColumn(colInvData, PL_SalesInvoices.ReleaseDate);
            doc.BindColumn(colMaster, PL_SalesInvoices.Master);
            doc.BindColumn(colSaleType, PL_SalesInvoices.Type);
            doc.BindColumn(colInvAddress, PL_SalesInvoices.Address);
            doc.BindColumn(colTotal, PL_SalesInvoices.Total);
        }

        private void dgwInvoice_DataGridViewDoubleClickCell(object sender, DataGridViewDoubleClickArgs e)
        {
            if (e.ColumnName == PL_SalesInvoices.InvoiceNo.Name)
            {
                Sales.Forms.SalesInvoice frm = (Sales.Forms.SalesInvoice)GlobalInfo.OpenForm("Plumber.Plumber.Sales.Forms.SalesInvoice", false);
                frm.FindRecord(new RadarSaleInvoicesParam(e.CellValue));
            }
            if (e.ColumnName == PL_SalesInvoices.Master.Name)
            {
                Masters.Forms.Masters frm = (Masters.Forms.Masters)GlobalInfo.OpenForm("Plumber.Plumber.Masters.Forms.Masters", false);
                string type = dgwInvoice.GetValue<string>(e.Row, PL_SalesInvoices.Type);
                frm.FindRecord(new RadarMasterParam(type, e.CellValue));
            }
        }

        public void OnPrepareAuxData()
        {
        }

        public void AutoResizeColumns()
        {
            dgwInvoice.AutoResizeColumns();
        }
    }
}